#include <poker.h>

void destroy_sdl_res(struct tractor_t *tractor)
{
    int i, j;

    SDL_FreeSurface(tractor->card_bg);
    SDL_FreeSurface(tractor->pic_btn_sell);
    SDL_FreeSurface(tractor->pic_btn_bottom);
    SDL_FreeSurface(tractor->pic_btn_lord);
    SDL_FreeSurface(tractor->pic_btn_cancel);
    SDL_FreeSurface(tractor->bg);

    for (i = 0; i < ASSISTANT; i++)
    {
        for (j = 0; j < TWO + 1; j++)
        {
            SDL_FreeSurface(tractor->card[i][j]);
            if (i == 0 && j == 1)
                break;
        }
    }
}

int load_sdl_res(struct tractor_t *tractor)
{
    int i, j;
    char buf[1024];

    tractor->card_bg = pic_load("pukeImage/bg.jpg");
    ERRP(NULL == tractor->card_bg, goto ERR1, 1, "pic_load bg!\n");

    tractor->bg = pic_load("pics/bg.jpg");
    tractor->pic_btn_sell = pic_load("pics/btn_sell.jpg");
    tractor->pic_btn_bottom = pic_load("pics/btn_bottom.jpg");
    tractor->pic_btn_lord = pic_load("pics/btn_lord.jpg");
    tractor->pic_btn_cancel = pic_load("pics/btn_cancel.jpg");

    for (i = 0; i < ASSISTANT; i++)
    {
        for (j = 0; j < TWO + 1; j++)
        {
            snprintf(buf, sizeof(buf), "pukeImage/%d_%d.jpg", i, j);
            /*printf("#%s#\n", buf);*/
            tractor->card[i][j] = pic_load(buf);
            ERRP(NULL == tractor->card[i][j], goto ERR2, 1, "pic_load card!\n");
            if (i == 0 && j == 1)
                break;
        }
    }
    return 0;
ERR2:
    destroy_sdl_res(tractor);
ERR1:
    return -1;
}

//用于调试打印玩家牌
static void _show_test(struct tractor_t *tractor) 
{
    int i, j;

    for (i = 0; i < 2; i++)
    {
        printf("player[%d] : ", i);
        /*tidy_card(tractor->lord, tractor->level[tractor->who], tractor->player[i]);*/
        for (j = 0; j < CARD_PLAYER; j++)
        {
            if (tractor->player[i].card[j] == NONE)
                continue;
            printf("%s ", poker_name[tractor->poker[tractor->player[i].card[j]].color][tractor->poker[tractor->player[i].card[j]].num]);
        }
        printf("\n");
    }
}

//显示网络玩家所有牌
void show_net_player(struct tractor_t *tractor)
{
    int i, j, first_row;
    SDL_Rect dst = {0, 0};
    int startx, starty, hand = CARD_PLAYER - CARD_HAND;

    //打印手中拿的牌
    /*draw_rect(tractor->sdl->screen, 0, 0, DESKTOP_W, DESKTOP_H / 2, 0x0);*/

    startx = (DESKTOP_W - ((tractor->player[1].hand_num - 1)* CARD_SPACE + CARD_W)) / 2;
    starty = MARGIN;

    for (i = 0, j = hand; i < tractor->player[1].hand_num; j++)
    {
        if (tractor->player[1].card[j] == NONE)
            continue;

        dst.x = startx + i++ * CARD_SPACE;
        dst.y = starty;

        SDL_BlitSurface(tractor->card[tractor->poker[tractor->player[1].card[j]].color]
                                     [tractor->poker[tractor->player[1].card[j]].num],
                        NULL, tractor->sdl->screen, &dst);
    }

    //打印桌面上排的牌
    startx = (DESKTOP_W - ((CARD_W + CARD_BORDER_SPACE) * CARD_ROW + (CARD_SPACE - CARD_BORDER_SPACE) * (CARD_ROW - 1))) / 2;
    starty = MARGIN + CARD_H * 2 + CARD_SPACE * 4;
    first_row = MARGIN + CARD_H + CARD_SPACE * 2;

    dst.x = startx;
    dst.y = starty;

    for (i = 0; i < hand; i++, dst.x += CARD_W + CARD_SPACE)
    {
        if (i != 0 && i % 8 == 0)
        {
            dst.x = startx;
            dst.y = first_row;
        }

        if (i == hand / 2)
            dst.y = starty; 

        if (tractor->player[1].card[i] == NONE)
            continue;

        if (tractor->poker[tractor->player[1].card[i]].hide)
            SDL_BlitSurface(tractor->card_bg, NULL, tractor->sdl->screen, &dst);
        else
        {

            dst.x += CARD_BORDER_SPACE;
            SDL_BlitSurface(tractor->card[tractor->poker[tractor->player[1].card[i]].color]
                                         [tractor->poker[tractor->player[1].card[i]].num], 
                            NULL, tractor->sdl->screen, &dst);
            dst.x -= CARD_BORDER_SPACE;
        }
    }
}

//显示本地玩家所有牌
void show_local_player(struct tractor_t *tractor)
{
    int i, j, first_row;
    SDL_Rect dst = {0, 0};
    int startx, starty, hand = CARD_PLAYER - CARD_HAND;

    //打印手中拿的牌
    /*draw_rect(tractor->sdl->screen, 0, DESKTOP_H / 2, DESKTOP_W, DESKTOP_H / 2, 0x333333);*/

    startx = (DESKTOP_W - ((tractor->player[0].hand_num - 1) * CARD_SPACE + CARD_W)) / 2;
    starty = DESKTOP_H - CARD_H - MARGIN;

    for (i = 0, j = hand; i < tractor->player[0].hand_num; j++)
    {
        if (tractor->player[0].card[j] == NONE)
            continue;

        dst.x = startx + i++ * CARD_SPACE;
        dst.y = starty;

        if (tractor->poker[tractor->player[0].card[j]].selection)
                dst.y -= CARD_SPACE;

        SDL_BlitSurface(tractor->card[tractor->poker[tractor->player[0].card[j]].color]
                                     [tractor->poker[tractor->player[0].card[j]].num], 
                        NULL, tractor->sdl->screen, &dst);
    }

    //打印桌面上排的牌
    startx = (DESKTOP_W - ((CARD_W + CARD_BORDER_SPACE) * CARD_ROW + (CARD_SPACE - CARD_BORDER_SPACE) * (CARD_ROW - 1))) / 2;
    starty = DESKTOP_H - MARGIN - CARD_H * 3 - CARD_SPACE * 4;
    first_row = DESKTOP_H - MARGIN - CARD_H * 2 - CARD_SPACE * 2;

    dst.x = startx;
    dst.y = starty;

    for (i = 0; i < hand; i++, dst.x += CARD_W + CARD_SPACE)
    {
        if (i != 0 && i % 8 == 0)
        {
            dst.x = startx;
            dst.y = first_row;
        }

        if (i == hand / 2)
            dst.y = starty; 

        if (tractor->player[0].card[i] == NONE)
            continue;

        if (tractor->poker[tractor->player[0].card[i]].hide)
            SDL_BlitSurface(tractor->card_bg, NULL, tractor->sdl->screen, &dst);
        else
        {

            if (tractor->poker[tractor->player[0].card[i]].selection)
                dst.y -= CARD_SPACE;

            dst.x += CARD_BORDER_SPACE;
            SDL_BlitSurface(tractor->card[tractor->poker[tractor->player[0].card[i]].color]
                                         [tractor->poker[tractor->player[0].card[i]].num], 
                            NULL, tractor->sdl->screen, &dst);
            dst.x -= CARD_BORDER_SPACE;

            if (tractor->poker[tractor->player[0].card[i]].selection)
                dst.y += CARD_SPACE;
        }
    }
}

void show_desktop(struct tractor_t *tractor)
{
    int i, startx, starty;
    SDL_Rect dst = {0, 0};

    startx = DESKTOP_W + CARD_SPACE;
    starty = DESKTOP_H / 2 - CARD_H / 2;

    for (i = 0; i < tractor->desktop_num; i++)
    {
        dst.x = startx + i * CARD_SPACE;
        dst.y = starty;

        SDL_BlitSurface(tractor->card[tractor->poker[tractor->desktop[i]].color]
                                     [tractor->poker[tractor->desktop[i]].num], 
                NULL, tractor->sdl->screen, &dst);
    }
}

void show_bottom(struct tractor_t *tractor)
{
    SDL_Rect dst = {0, MARGIN};
    int startx, i;

    startx = WIDTH - CARD_SPACE * CARD_BOTTOM - CARD_W;
    dst.x = startx;

    if (!tractor->who)
        dst.y = DESKTOP_H - CARD_H - MARGIN;

    if (tractor->ck_bottom)
    {
        for (i = 0; i < CARD_BOTTOM; i++)
        {
            dst.x = startx + i * CARD_SPACE;
            SDL_BlitSurface(tractor->card_bg, NULL, tractor->sdl->screen, &dst);
        }
    }

    if (tractor->who != -1)
        SDL_BlitSurface(tractor->card[tractor->lord]
                                    [tractor->player[tractor->who].level], 
                            NULL, tractor->sdl->screen, &dst);
}

void show_tractor(struct tractor_t *tractor)
{
    SDL_Rect dst;
    int startx, starty;

    /*draw_rect(tractor->sdl->screen, 0, 0, DESKTOP_W, DESKTOP_H, 0x0);*/
    SDL_SoftStretch(tractor->bg, NULL, tractor->sdl->screen, NULL);
    /*_show_test(tractor);*/

    show_net_player(tractor);           //显示网络玩家所有牌
    show_local_player(tractor);         //显示本地玩家所有牌
    show_desktop(tractor);              //显示桌面出的牌
    show_bottom(tractor);               //显示底牌和此次是谁的什么主

    startx = DESKTOP_W - (DESKTOP_W - (tractor->player[0].hand_num - 1) * CARD_SPACE - CARD_W) / 2 + CARD_SPACE;
    starty = DESKTOP_H - CARD_H - MARGIN - CARD_SPACE;

    if (tractor->ck_bottom)
    {
        button_show(tractor->btn_sell, startx, starty);   //显示出牌按钮
        //显示取消按钮
        button_show(tractor->btn_cancel, 
                    startx + tractor->btn_cancel->button->w + CARD_SPACE, starty);
    }
    else if (tractor->who != -1)
        button_show(tractor->btn_bottom, startx, starty);

    SDL_Flip(tractor->sdl->screen);
}

